package com.ruoyi.project.poem.stat.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.poem.stat.domain.PkStat;
import com.ruoyi.project.poem.stat.service.IPkStatService;
import com.ruoyi.project.report.stats.domain.IpStats;

/**
 * 诗词PK统计Controller
 * 
 * @author shixiangwen
 * @date 2020-02-03
 */
@Controller
@RequestMapping("/poem/stat")
public class PkStatController extends BaseController {
	private String prefix = "poem/stat";

	@Autowired
	private IPkStatService pkStatService;

	@RequiresPermissions("poem:stat:view")
	@GetMapping()
	public String stat() {
		return prefix + "/stat";
	}

	/**
	 * 查询诗词PK统计列表
	 */
	@RequiresPermissions("poem:stat:list")
	@GetMapping("/chat")
	public String list(ModelMap mmap) {

		List<String> dates = new ArrayList<>();
		List<Long> userData = new ArrayList<>();
		List<Long> roomData = new ArrayList<>();
		List<Long> finishData = new ArrayList<>();

		Date now = DateUtils.getNowDate();
		for (int i = 30; i >= 0; i--) {

			String date = DateUtils.dateTime(DateUtils.addDays(now, i * -1));
			IpStats query = new IpStats();
			query.setDate(date);

			PkStat pkStat = pkStatService.selectPkStatByDate(date);
			if (pkStat != null) {
				dates.add(date);
				userData.add(pkStat.getUserCount());
				roomData.add(pkStat.getPkCount());
				finishData.add(pkStat.getFinishCount());
			}
		}

		mmap.put("dates", dates);
		mmap.put("userData", userData);
		mmap.put("roomData", roomData);
		mmap.put("finishData", finishData);

		return prefix + "/chat";
	}

	/**
	 * 导出诗词PK统计列表
	 */
	@RequiresPermissions("poem:stat:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(PkStat pkStat) {
		List<PkStat> list = pkStatService.selectPkStatList(pkStat);
		ExcelUtil<PkStat> util = new ExcelUtil<PkStat>(PkStat.class);
		return util.exportExcel(list, "stat");
	}

	/**
	 * 新增诗词PK统计
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存诗词PK统计
	 */
	@RequiresPermissions("poem:stat:add")
	@Log(title = "诗词PK统计", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PkStat pkStat) {
		return toAjax(pkStatService.insertPkStat(pkStat));
	}

	/**
	 * 修改诗词PK统计
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		PkStat pkStat = pkStatService.selectPkStatById(id);
		mmap.put("pkStat", pkStat);
		return prefix + "/edit";
	}

	/**
	 * 修改保存诗词PK统计
	 */
	@RequiresPermissions("poem:stat:edit")
	@Log(title = "诗词PK统计", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PkStat pkStat) {
		return toAjax(pkStatService.updatePkStat(pkStat));
	}

	/**
	 * 删除诗词PK统计
	 */
	@RequiresPermissions("poem:stat:remove")
	@Log(title = "诗词PK统计", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(pkStatService.deletePkStatByIds(ids));
	}
}
